﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Docsbox.Layouts.Docsbox
{
    public partial class projectDocuments : LayoutsPageBase
    {
        private static string _listId;
        private static string _itemId;
        private static string _filter;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _listId = Request.QueryString["List"];
                _itemId = Request.QueryString["ID"];

                LoadDocumentList();
            }
            LoadList();
                
        }

        private void LoadDocumentList()
        {
            if(!String.IsNullOrEmpty(_listId) && !string.IsNullOrEmpty(_itemId)){
                Guid listId = new Guid(_listId);
                int itemId = Convert.ToInt32(_itemId);
                SPWeb current = this.Web;
                SPList projects = current.Lists[listId];
                SPListItem item = projects.GetItemById(itemId);
                _filter = item["Title"].ToString();
            }
        }
        private void LoadList()
        {
            SPWeb current = this.Web;
            SPList documents = current.Lists[Common.ListDocuments];
            SPQuery query = new SPQuery(documents.DefaultView);
            query.Query = GetQueryString(_filter);
            query.QueryThrottleMode = SPQueryThrottleOption.Override;

            projectDocumentListView.List = documents;
            projectDocumentListView.Query = query;
        }
        private string GetQueryString(string filter)
        {
            String query = @"<Where><Eq><FieldRef Name='{1}' /><Value Type='Lookup'>{0}</Value></Eq></Where>";
            return string.Format(query, filter,Common.FieldAccountLookup);
        }

        private void ClosePopUp()
        {
            Context.Response.Write("<Script type='text/javascript'>window.frameElement.commitPopup();</script>");
            Context.Response.Flush();
            Context.Response.End();
        }

    }
}
